package com.qzxy.premiumhotelsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzxy.premiumhotelsystem.common.constant.RoomStatusConstant;
import com.qzxy.premiumhotelsystem.domain.dto.RoomProDTO;
import com.qzxy.premiumhotelsystem.domain.dto.RoomStockPageDTO;
import com.qzxy.premiumhotelsystem.common.result.PageResult;
import com.qzxy.premiumhotelsystem.common.result.Result;
import com.qzxy.premiumhotelsystem.domain.dto.RoomsDTO;
import com.qzxy.premiumhotelsystem.domain.dto.RoomsPageDTO;
import com.qzxy.premiumhotelsystem.domain.entity.RoomTypes;
import com.qzxy.premiumhotelsystem.domain.entity.Rooms;
import com.qzxy.premiumhotelsystem.domain.vo.RoomsVO;
import com.qzxy.premiumhotelsystem.mapper.RoomTypesMapper;
import com.qzxy.premiumhotelsystem.mapper.RoomsMapper;
import com.qzxy.premiumhotelsystem.service.RoomsService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
* @author 14912
* @description 针对表【rooms】的数据库操作Service实现
* @createDate 2024-09-12 15:55:14
*/
@Service
@Slf4j
public class RoomsServiceImpl extends ServiceImpl<RoomsMapper, Rooms>
    implements RoomsService{

    @Resource
    private RoomsMapper roomsMapper;
    @Resource
    private RoomTypesMapper roomTypesMapper;

    @Override
    public Result addRoom(RoomsDTO roomsDTO) {
        try {
            Rooms rooms = BeanUtil.toBean(roomsDTO, Rooms.class);
            rooms.setStatus(RoomStatusConstant.AVAILABLE);
            roomsMapper.insert(rooms);
        }catch (DuplicateKeyException e){
            throw new RuntimeException("房间号唯一索引冲突");
        }
        return  Result.ok("添加房间成功");

    }

    @Override
    public Result selectByRoomId(Integer roomId) {
        Rooms rooms = roomsMapper.selectById(roomId);
        RoomTypes roomTypes = roomTypesMapper.selectById(rooms.getRoomTypeId());
        RoomsVO roomsVO = RoomsVO.builder().roomId(rooms.getRoomId())
                .roomNumber(rooms.getRoomNumber())
                .roomType(roomTypes.getRoomType())
                .price(roomTypes.getPrice())
                .status(rooms.getStatus())
                .description(rooms.getDescription())
                .roomUrl(roomTypes.getRoomUrl())
                .isAdvice(roomTypes.getIsAdvice())
                .typeExplain(roomTypes.getTypeExplain())
                .build();

        return Result.ok(roomsVO);
    }


    @Override
    public Result<PageResult> pageQuery(RoomsPageDTO roomsPageDTO) {

        log.info("RoomsPageDTO:{}",roomsPageDTO);

        IPage<RoomsVO> page = new Page<>(roomsPageDTO.getPageNo(),roomsPageDTO.getPageSize());

        roomsMapper.pageQueryWithRoomType(page,roomsPageDTO);

        return Result.ok(new PageResult(page.getTotal(),page.getRecords()));
    }

    @Override
    public void updateRoomStatusByRoomNumber(String roomNumber, String status) {
        LambdaUpdateWrapper<Rooms> luw = new LambdaUpdateWrapper<>();
        luw.eq(Rooms::getRoomNumber,roomNumber);
        luw.set(Rooms::getStatus,status);
        roomsMapper.update(luw);
    }



    @Override
    public Result getRoomStatusList() {
        Map<Integer, String> statusMap = RoomStatusConstant.STATUS_MAP;
        return Result.ok(statusMap.values());
    }

    @Override
    public Result countRooms() {
        return Result.ok(roomsMapper.countRooms());
    }

    @Override
    public Result getRoomsInType(Integer roomTypeId) {
        LambdaQueryWrapper<Rooms> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Rooms::getRoomTypeId,roomTypeId);
        lqw.eq(Rooms::getStatus, RoomStatusConstant.AVAILABLE);
        List<Rooms> rooms = roomsMapper.selectList(lqw);
        return Result.ok(rooms);

    }

    @Override
    public List<RoomProDTO> allList() {
        return roomsMapper.allList();
    }

}




